import vue from '@vitejs/plugin-vue'
import vueJsx from "@vitejs/plugin-vue-jsx";
import path from 'path'

import { parseEnv } from './core/util'
import { defineConfig, loadEnv } from 'vite'
import prismjs from 'vite-plugin-prismjs'
import autoImport from './core/auto-import'
import mock from './core/mock'


export default defineConfig(({ command, mode }) => {
  const isBuild = command == 'build'
  const env = parseEnv(loadEnv(mode, process.cwd()))
  console.log(env.VITE_API_URL)
  return {
    plugins: [
      ...autoImport,
      vue(),
      vueJsx(),
      mock(isBuild, env),
      prismjs({
        languages: 'all',
      }),
    ],
    base: isBuild ? '/dist' : '/',
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
        '#': path.resolve(__dirname, 'types'),
      },
    },
    // 指定输出文件夹
    build: {
      emptyOutDir: true,
      // 输出路径
      outDir: 'dist',
      rollupOptions: {
        output: {
          manualChunks(id: string) {
            if (id.includes('node_modules')) {
              return id.split('/node_modules/').pop()?.split('/')[0]
            }
          },
        },
      },
    },
    server: {
      host: true,
      proxy: {
        '/api': {
          target: env.VITE_API_URL,
          changeOrigin: true,
        },
      },
    },
  }
})
